package com.ibix.netty.communion;

import com.ibix.netty.tools.PrintStatus;
import com.ibix.print.PrintBean;
import com.ibix.print.PrintUtil;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Created by Administrator on 2018/1/23.
 */
public class MessageHandle {
    protected static final Logger LOGGER = LoggerFactory.getLogger(MessageHandle.class);
    public static void sendMessage(ChannelHandlerContext ctx, Object object){
        ctx.channel().write(object);
        ctx.channel().writeAndFlush("<RE>");
    }

    public static void distributeMessage(ChannelHandlerContext ctx,Object object){
        if(object.getClass().getName().equals(MessagePing.class.getName()))
        {

        }else
        {
            LOGGER.info("Re:"+ctx.channel().remoteAddress().toString()+": "+object.toString());
        }
        if(object.getClass().equals(PrintBean.class))
        {
            PrintBean printBean = (PrintBean) object;
            PrintStatus printStatus = new PrintStatus();
            printStatus.setId(printBean.id());
            printStatus.setStatus(PrintStatus.Status.HANDLE_OK);
            try {
                PrintUtil printUtil = new PrintUtil();
                printUtil.print(printBean);
            }catch (Exception e)
            {
                printStatus.setStatus(PrintStatus.Status.HANDLE_FAIL);
            }
            sendMessage(ctx,printStatus);
        }
    }
}
